/* cl.ext.daterange 0.1*/
(
    function (factory) {
        if (typeof define === 'function' && define.amd) {
            define(['jquery',"moment","DateRange"], function($,momentjs) {
               factory($,momentjs);
            });
        } else if (typeof module === 'object' && module.exports) {
            var momentjs = require('moment');
            var rangejs = require('DateRange');
            module.exports = function( root, jQuery ) {
                if ( jQuery === undefined ) {
                    if ( typeof window !== 'undefined' ) {
                        jQuery = require('jquery');
                    }
                    else {
                        jQuery = require('jquery')(root);
                    }
                }
                factory(jQuery,momentjs);
            };
        } else {
            factory(jQuery, moment);
        }
    }(
    function ($,moment){
        var defaultExtInfo={changeCallback:null}; //type: date|dateStartEnd|range
        var defaults={
            startDate: moment().subtract(7, 'days'),
            endDate:  moment().subtract(1, 'days'),
            opens: "left",
            locale:{"format":"YYYY-MM-DD","separator":" ~ ","applyLabel":"确定","cancelLabel":"取消","customRangeLabel":"自定义"},
            alwaysShowCalendars:true,
            applyClass:'btn-default',
            cancelClass: 'btn-graye8',
            ranges: {
                '今天': [moment(), moment()],
                '昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                '过去7天': [moment().subtract(7, 'days'), moment().subtract(1, 'days')],
                '过去30天': [moment().subtract(30, 'days'), moment().subtract(1, 'days')],
                '本月': [moment().startOf('month'), moment().endOf('month')],
                '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            },
            //maxDate:"2016-10-11",
            infoExt:defaultExtInfo
        };
        var $modalCommon={
            getDefaults :function () {
                return defaults
            },
            getOptions : function (elementData,options) {
                options=$.ExtCommon.isCheckEmpty(options)?{}:options;
                var optionBase=$.ExtCommon.initMergeJson(elementData,options);
                optionBase =$.ExtCommon.initMergeJson(this.getDefaults(),optionBase);
                return optionBase
            }
        };
        var DateRangExtModal = function(element, options) {
            var self = this;
            self.element = $(element);
            self.optionBase = $modalCommon.getOptions(self.element.data(), options);
            self.init();
        };
        DateRangExtModal.prototype = {
            init: function () {
                var self = this;
                var fun=self.element.data('ExtDateRang');
                self.element.daterangepicker(self.optionBase, function (start, end, label) {
                    if (self.optionBase.infoExt.changeCallback){self.optionBase.infoExt.changeCallback(start, end, label)}
                });
                if(fun){self.element.data('ExtDateRang',fun);}
                self.element.parent().find("i").off("click").on("click",function() {
                    self.element.click()
                });
                setTimeout(function(){
                    $(".daterangepicker :text.input-mini").prop("disabled", true);
                },300);
            },
            getDateRangeValue: function () {
                var arr = [];
                var value = $.trim(this.element.val());
                if (value.length > 0 && value.indexOf("~") > -1) {
                    var arrtemp = value.split("~");
                    if (arrtemp.length > 0) arr.push($.trim(arrtemp[0]));
                    if (arrtemp.length > 1) arr.push($.trim(arrtemp[1]));
                }
                return arr;
            },
            setDateRangeValue:function(times){
                var self = this;
                var arrTime=[];
                var t = $.type(times);
                if(t=="array"){
                    arrTime=times;
                    if(times.length==1){arrTime.push(times[0]);}
                }else{
                    arrTime.push(times);
                    arrTime.push(times);
                }
                if(arrTime.length==0){
                    arrTime.push(self.initFormat());
                    arrTime.push(self.initFormat());
                }
                self.element.data('daterangepicker').setStartDate(arrTime[0]);
                self.element.data('daterangepicker').setEndDate(arrTime[1]);
            },
            setOptions:function(options){
                var self = this;
                //self.destroy();
                self.optionBase =  $.ExtCommon.initMergeJson(self.optionBase,options);
                //setTimeout()
                //self.element.data('ExtDateRang', new DateRangExtModal(self.element, options));
                self.init();
            },
            destroy: function () {
                this.element.data('daterangepicker').remove()
            }
        };
        $.fn.ExtDateRang = function(options) {
            var dataFun;
            this.each(function() {
                var el = $(this);
                var tempData=el.data('ExtDateRang');
                if (tempData) {
                    if (typeof  options == "string") {
                        if (tempData[options]) {
                            dataFun =tempData[options]()
                        } else {
                            dataFun = tempData
                        }
                    } else {
                        dataFun = tempData
                    }
                } else {
                    dataFun = new DateRangExtModal(el, options);
                    el.data('ExtDateRang', dataFun);
                }
            });
            return dataFun;
        };
        return DateRangExtModal;
    })
);